import 'package:dingtalk/core/model/message_setting_model.dart';
import 'package:dingtalk/core/values/values.dart';
import 'package:dingtalk/mock/mock_message.dart';
import 'package:dingtalk/ui/widgets/common_list_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class THMessageSettingScreen extends StatefulWidget {
  static final String routeName = "messageSetting";

  @override
  _THMessageSettingScreenState createState() => _THMessageSettingScreenState();
}

class _THMessageSettingScreenState extends State<THMessageSettingScreen>
    with AutomaticKeepAliveClientMixin {
  List<THMessageSettingModel> _messageSettingItems = [];

  @override
  void initState() {
    _messageSettingItems = THMockMessage.messageSettingItems;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    bool isOpenAll =
        _messageSettingItems.every((element) => element.isOpened == 1);
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: <Widget>[
            Text(
              "消息直通车",
              style: TextStyle(color: kColor33, fontSize: kSize32),
            )
          ],
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Container(
              height: kSize500,
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage(
                          'assets/images/live_show_default_cover.png'))),
            ),
            Column(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                THCommonListItem(
                  title: Padding(
                    padding: EdgeInsets.only(left: kSize20),
                    child: Text(
                      "消息直通车",
                      style: TextStyle(color: kColor33, fontSize: kSize32),
                    ),
                  ),
                  trailing: Container(
                    height: kSize80,
                    child: CupertinoSwitch(
                      value: isOpenAll,
                      activeColor: kPrimaryColor,
                      onChanged: (bool state) {
                        _messageSettingItems =
                            _messageSettingItems.map((element) {
                          return THMessageSettingModel(
                              iconPath: element.iconPath,
                              title: element.title,
                              activeColor: element.activeColor,
                              isOpened: state ? 1 : 0);
                        }).toList();
                        this.setState(() {
                          _messageSettingItems = _messageSettingItems;
                        });
                      },
                    ),
                  ),
                  showDivider: false,
                ),
                Container(
                  padding: EdgeInsets.symmetric(vertical: kSize36),
                  child: Text(
                    "开启后，你可以在首页看到对应类别消息的快捷入口",
                    style: TextStyle(color: kColor99),
                  ),
                ),
                ListView.builder(
                  shrinkWrap: true,
                  itemCount: _messageSettingItems.length,
                  itemBuilder: (BuildContext context, int index) {
                    THMessageSettingModel model = _messageSettingItems[index];
                    return THCommonListItem(
                      leading: SvgPicture.asset(
                        model.iconPath,
                        width: kSize38,
                        color: model.activeColor,
                      ),
                      title: Text(
                        model.title,
                        style: TextStyle(color: kColor33, fontSize: kSize32),
                      ),
                      trailing: Container(
                        height: kSize80,
                        child: CupertinoSwitch(
                          value: model.isOpened == 1,
                          activeColor: kPrimaryColor,
                          onChanged: (bool state) {
                            _messageSettingItems[index].isOpened =
                                state ? 1 : 0;
                            this.setState(() {
                              _messageSettingItems = _messageSettingItems;
                            });
                          },
                        ),
                      ),
                      showDivider: index != _messageSettingItems.length,
                    );
                  },
                  padding: EdgeInsets.zero,
                  physics: NeverScrollableScrollPhysics(),
                ),
              ],
            ),
            Container(
              height: kSize100,
              alignment: Alignment.center,
              child: Text("意见反馈"),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
